package cn.itcast.erp.service.impl;

import cn.itcast.erp.dao.mapper.SupplierMapper;
import cn.itcast.erp.entity.Dep;
import cn.itcast.erp.entity.Goodstype;
import cn.itcast.erp.entity.Supplier;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.service.SupplierService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
 * 供应商 service层实现类
 * @author 罗宇航
 * @date 2019/6/5
 */
@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {
    @Resource
    private SupplierMapper supplierMapper;

    @Override
    public int insertSupplier(Supplier supplier) {
        if (supplier != null) {
            int i = this.supplierMapper.insertSupplier(supplier);
            return i;
        }
        return -1;
    }

    @Override
    public int deleteSupplier(Long uuid) {
        if (uuid != null) {
            int i = this.supplierMapper.deleteSupplier(uuid);
            return i;
        }
        return -1;

    }

    @Override
    public int updateSupplier(Supplier supplier) {
        if (supplier != null) {
            int i = this.supplierMapper.updateSupplier(supplier);
            return i;
        }
        return -1;

    }


    @Override
    public AjaxResult findSupplier(Supplier supplier) {
        int total = this.supplierMapper.count(supplier);
        List<Supplier> rows = this.supplierMapper.findSupplier(supplier);
        AjaxResult ar = new AjaxResult();
        ar.setTotal(total);
        ar.setRows(rows);
        return ar;
    }

    @Override
    public AjaxResult findAllSupplier(Supplier supplier) {
        List<Supplier> allSupplier = this.supplierMapper.findAllSupplier(supplier);
        AjaxResult ar = new AjaxResult();
        ar.setTotal(allSupplier.size());
        ar.setRows(allSupplier);
        return ar;
    }

    @Override
    public Supplier findSupplierById(Long uuid) {
        return this.supplierMapper.findSupplierById (uuid);
    }

    // 导出Excel
    @Override
    public InputStream getAllSupplierEx(int type) {
        //添加数据
        List<Supplier> supplier = supplierMapper.getAllSupplierEx(type);

        HSSFWorkbook sheets = new HSSFWorkbook();  // 定义一个新的工作簿
        Sheet sheet = sheets.createSheet("商品类型信息");  // 创建第一个新的Sheet页


        //创建第一行（也可以成为表头）
        Row row1 = sheet.createRow(0);
        sheet.setColumnWidth(0, 20 * 256);
        sheet.setColumnWidth(1, 30 * 256);
        sheet.setColumnWidth(2, 20 * 256);
        sheet.setColumnWidth(3, 30 * 256);
        sheet.setColumnWidth(4, 20 * 256);
        sheet.setColumnWidth(5, 30 * 256);


        //给表头第一行一次创建单元格
        String[] headers = {"编号","名称","联系地址","联系人","联系电话","邮件地址"};
        for(int i = 0; i < headers.length; i++){
            row1.createCell(i).setCellValue(headers[i]);
            row1.setHeightInPoints(30);//设置行的高度是30
        }


        //向单元格里添加数据
        for(short i=0;i<supplier.size();i++){
            row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(supplier.get(i).getUuid());
            row1.createCell(1).setCellValue(supplier.get(i).getName());
            row1.createCell(2).setCellValue(supplier.get(i).getAddress());
            row1.createCell(3).setCellValue(supplier.get(i).getContact());
            row1.createCell(4).setCellValue(supplier.get(i).getTele());
            row1.createCell(5).setCellValue(supplier.get(i).getEmail());
            row1.setHeightInPoints(30);//设置行的高度是30
        }
        //字节流缓存区
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        try {
            sheets.write(bo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ByteArrayInputStream(bo.toByteArray());
    }

}
